package com.basic.business.kanban.controller;

import com.basic.business.goodsIndicators.service.ITGoodsIndicatorsService;
import com.basic.business.kanban.vo.ProductDateEchelonExcelRespVo;
import com.basic.business.kanban.vo.ProductEchelonExcelRespVo;
import com.basic.business.kanban.vo.ProductEchelonVo;
import com.basic.business.kanban.vo.ProductMonthEchelonVo;
import com.basic.common.annotation.FileExport;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.utils.easyExcel.EasyExcelUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 看板
 */
@RestController
@RequestMapping("/business/bulletinBoard")
public class BulletinBoardController {

    @Resource
    private ITGoodsIndicatorsService tGoodsIndicatorsService;

    /**
     * 获取产品梯队看板数据
     *
     * @param productEchelonVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:productEchelonBoard:list')")
    @GetMapping("/getProductEchelon")
    public AjaxResult getProductEchelon(ProductEchelonVo productEchelonVo) {
        List<Map<String, Object>> list = tGoodsIndicatorsService.getProductEchelon(productEchelonVo);

        return AjaxResult.success(list);
    }

    /**
     * 获取产品梯队月份看板数据
     *
     * @param productMonthEchelonVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:productMonthEchelonBoard:list')")
    @GetMapping("/getProductMonthEchelon")
    public AjaxResult getProductMonthEchelon(ProductMonthEchelonVo productMonthEchelonVo) {
        List<Map<String, Object>> list = tGoodsIndicatorsService.getProductMonthEchelon(productMonthEchelonVo);
        return AjaxResult.success(list);
    }

    /**
     * 导出商品梯队看板数据excel
     */
    @PreAuthorize("@ss.hasPermi('business:productEchelonBoard:export')")
    @FileExport(fileName = "产品梯队数据.xlsx")
    @PostMapping("/exportProductEchelon")
    public String exportProductEchelon(@RequestBody ProductEchelonVo productEchelonVo) throws IOException {
        List<Map<String, Object>> list = tGoodsIndicatorsService.getProductEchelon(productEchelonVo);

        List<ProductEchelonExcelRespVo> excelList = new ArrayList<>();

        for (Map<String, Object> map : list) {
            ProductEchelonExcelRespVo excel = new ProductEchelonExcelRespVo();

            if (map.get("store_name") != null) {
                excel.setLabel(map.get("store_name").toString());
            } else if (map.get("goodsLabel") != null) {
                excel.setLabel(map.get("goodsLabel").toString());
            } else if (map.get("store_leader") != null) {
                excel.setLabel(map.get("store_leader").toString());
            } else if (map.get("div_name") != null) {
                excel.setLabel(map.get("div_name").toString());
            }
            excel.setCount_500_plus(Integer.valueOf(map.get("count_500_plus").toString()));
            excel.setCount_200_500(Integer.valueOf(map.get("count_200_500").toString()));
            excel.setCount_100_200(Integer.valueOf(map.get("count_100_200").toString()));
            excel.setCount_50_100(Integer.valueOf(map.get("count_50_100").toString()));
            excel.setCount_30_50(Integer.valueOf(map.get("count_30_50").toString()));
            excel.setCount_10_30(Integer.valueOf(map.get("count_10_30").toString()));
            excel.setCount_3_10(Integer.valueOf(map.get("count_3_10").toString()));
            excel.setCount_1_3(Integer.valueOf(map.get("count_1_3").toString()));
            excel.setCount_0(Integer.valueOf(map.get("count_0").toString()));
            excel.setCount_all(Integer.valueOf(map.get("count_all").toString()));
            excel.setSalesTurnoverRate(map.get("salesTurnoverRate").toString() + "%");
            excel.setSalesTurnoverRateThree(map.get("salesTurnoverRateThree").toString() + "%");
            excel.setNetSalesRevenue(map.get("netSalesRevenue").toString());
            excel.setPromotionRatio(map.get("promotionRatio").toString());
            excel.setCustomerOrderPrice(map.get("customerOrderPrice").toString());
            excel.setDynamicSalesThree(map.get("dynamicSalesThree").toString());
            excelList.add(excel);
        }

        return EasyExcelUtils.excelExport(ProductEchelonExcelRespVo.class, "产品梯队数据", excelList);
    }

    /**
     * 导出商品梯队日期看板数据excel
     */
    @PreAuthorize("@ss.hasPermi('business:productMonthEchelonBoard:export')")
    @FileExport(fileName = "产品梯队日期数据.xlsx")
    @PostMapping("/exportProductDateEchelon")
    public String exportProductDateEchelon(@RequestBody ProductMonthEchelonVo productMonthEchelonVo) throws IOException {
        List<Map<String, Object>> list = tGoodsIndicatorsService.getProductMonthEchelon(productMonthEchelonVo);

        List<ProductDateEchelonExcelRespVo> excelList = new ArrayList<>();

        for (Map<String, Object> map : list) {
            ProductDateEchelonExcelRespVo excel = new ProductDateEchelonExcelRespVo();
            excel.setStatDate(map.get("statDate").toString());
            excel.setCount_500_plus(Integer.valueOf(map.get("count_500_plus").toString()));
            excel.setCount_200_500(Integer.valueOf(map.get("count_200_500").toString()));
            excel.setCount_100_200(Integer.valueOf(map.get("count_100_200").toString()));
            excel.setCount_50_100(Integer.valueOf(map.get("count_50_100").toString()));
            excel.setCount_30_50(Integer.valueOf(map.get("count_30_50").toString()));
            excel.setCount_10_30(Integer.valueOf(map.get("count_10_30").toString()));
            excel.setCount_3_10(Integer.valueOf(map.get("count_3_10").toString()));
            excel.setCount_1_3(Integer.valueOf(map.get("count_1_3").toString()));
            excel.setCount_0(Integer.valueOf(map.get("count_0").toString()));
            excel.setCount_all(Integer.valueOf(map.get("count_all").toString()));
            excel.setSalesTurnoverRate(map.get("salesTurnoverRate").toString() + "%");
            excel.setSalesTurnoverRateThree(map.get("salesTurnoverRateThree").toString() + "%");
            excel.setNetSalesRevenue(map.get("netSalesRevenue").toString());
            excel.setPromotionRatio(map.get("promotionRatio").toString());
            excel.setCustomerOrderPrice(map.get("customerOrderPrice").toString());
            excel.setDynamicSalesThree(map.get("dynamicSalesThree").toString());
            excelList.add(excel);
        }

        return EasyExcelUtils.excelExport(ProductDateEchelonExcelRespVo.class, "产品梯队日期数据", excelList);
    }
}
